package com.lzy.blog.interceptor;

import com.lzy.blog.enums.ResultEnum;
import com.lzy.blog.exception.BlogException;
import com.lzy.blog.pojo.Admin;
import com.lzy.blog.utils.ShiroUtils;
import com.lzy.blog.utils.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: lzy_blog
 * @description: 拦截器
 * @author: lzy
 * @create: 2020-06-06 23:30
 **/
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取token
        String token = request.getHeader("Authorization");
        if(StringUtils.isBlank(token)){
            //如果token不为空，就获取当前登录的用户
            Admin loginAdmin = ShiroUtils.getLoginAdmin();
            if(null != loginAdmin){
                //如果loginadmin不为空，则说明token有效
                return true;
            }
        }

        //没有登录
        throw new BlogException(ResultEnum.NOT_LOGIN);
//        return false;
    }
}
